package com.fosun.sprcore.tools.generatecode.template;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 


import org.apache.commons.lang3.StringUtils;

import com.fosun.sprcore.tools.generatecode.base.BaseTemplate;
import com.fosun.sprcore.tools.generatecode.base.GenerateCodeMain;

/**
 * 生成Control、Servcie、Dao
 * 
 * @author chensm@fosun.com Jun 21, 2017
 */
public class ModuleFileTemplate extends BaseTemplate {
	private final static String TEMPLATE_PATH = "sprcore/ModuleFileTemplate/";
	private String moduleName;
	private String packageName;
	private List<ServcieModel> servcieModelList = new ArrayList<ModuleFileTemplate.ServcieModel>();

	@Override
	public void generate(GenerateCodeMain main) throws Exception {
		Map data = new HashMap<String, String>();
		data.put("projectWar", projectWar);
		data.put("moduleName", moduleName);
		data.put("packageName", packageName);
		String moduleNameU = StringUtils.capitalize(moduleName);
		data.put("moduleNameU", moduleNameU);
		data.put("servcieModelList", servcieModelList);
		
		main.mkdir("/java_service/"+moduleName + "/bussiness/impl");
		main.mkdir("/java_service/"+moduleName + "/bussiness/model");
		
		main.generate(TEMPLATE_PATH + "control/Control.java", "/java_service/"+moduleName + "/control/" + moduleNameU + "Control.java", data);
		main.generate(TEMPLATE_PATH + "service/Service.java", "/java_service/"+moduleName + "/service/" + moduleNameU + "Service.java", data);
		main.generate(TEMPLATE_PATH + "service/impl/ServiceImpl.java","/java_service/"+moduleName + "/service/impl/" + moduleNameU + "ServiceImpl.java", data);
		main.generate(TEMPLATE_PATH + "dao/Dao.java", "/java_service/"+moduleName + "/dao/" + moduleNameU + "Dao.java", data);
		main.generate(TEMPLATE_PATH + "dao/xml/Mapper.xml", "/java_service/"+moduleName + "/dao/xml/" + moduleNameU + "Mapper.xml", data);
		
		for(int i=0,j=servcieModelList.size();i<j;i++){
			ModuleFileTemplate.ServcieModel model = (ModuleFileTemplate.ServcieModel)servcieModelList.get(i);
			data.put("service_methodName", model.getMethodName());
			data.put("service_methodNameU", model.getMethodNameU());
			data.put("service_submitMethod", model.getSubmitMethod());
			data.put("service_methodType", model.getMethodType());
			main.generate(TEMPLATE_PATH + "service/model/SpModel.java","/java_service/"+moduleName + "/service/model/" + model.getMethodNameU() + "SpModel.java", data);
			main.generate(TEMPLATE_PATH + "service/model/SrModel.java","/java_service/"+moduleName + "/service/model/" + model.getMethodNameU() + "SrModel.java", data);
		}

	}

	public String getModuleName() {
		return moduleName;
	}

	public void setModuleName(String moduleName) {
		this.moduleName = moduleName;
	}

	public String getPackageName() {
		return packageName;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	public void addServcieModelList(ServcieModel model) {
		servcieModelList.add(model);
	}

	public static class ServcieModel {
		public static final String SUBMITMETHOD_GET = "GET";
		public static final String SUBMITMETHOD_POST = "POST";
		public static final String METHODTYPE_LIST = "LIST";
		public ServcieModel(String methodName, String submitMethod,String methodType) {
			this.methodName = methodName;
			this.submitMethod = submitMethod;
			this.methodType = methodType==null?"":methodType;	
		}

		private String methodName;
		private String submitMethod;
		private String methodType;
		private String methodNameU;
		
 
		public String getMethodName() {
			return methodName;
		}
		public String getMethodNameU() {
			return StringUtils.capitalize(methodName);
		}
		public void setMethodName(String methodName) {
			this.methodName = methodName;
		}

 
		public String getMethodType() {
			return methodType;
		}
		public void setMethodType(String methodType) {
			this.methodType = methodType;
		}
		public void setMethodNameU(String methodNameU) {
			this.methodNameU = methodNameU;
		}
		public String getSubmitMethod() {
			return submitMethod;
		}
		public void setSubmitMethod(String submitMethod) {
			this.submitMethod = submitMethod;
		}


	}
}
